//
//  ViewController.m
//  27-block练习播放按钮
//
//  Created by 殷殷明静 on 16/8/19.
//  Copyright © 2016年 殷明静. All rights reserved.
//

#import "ViewController.h"
#import "PlayView.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVQueuePlayer *queuePlayer;
@property (nonatomic, strong) AVPlayerItem *songItem;
@property (nonatomic, strong) AVPlayerItem *songItem1;
@property (nonatomic, strong) AVPlayerItem *songItem2;
@property (nonatomic, strong) NSArray *items;
@property (weak, nonatomic) IBOutlet PlayView *palyView;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (nonatomic, assign) NSInteger index;
- (IBAction)sliderAct:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.index = 0;
    [self.palyView play:^(UIButton *play) {
        [_queuePlayer play];
        NSLog(@"播放");
    } pause:^(UIButton *pause) {
        [_queuePlayer pause];
        NSLog(@"暂停");
        
    }];
   [self.palyView lastPlay:^(UIButton *last_btn) {
       if (_index == 0) {
           _index = _items.count - 1;
       } else {
           _index--;
       }
       [self palyAtIndex:_index];
       NSLog(@"上一首");
   }];
    [self.palyView nextPlay:^(UIButton *next_btn) {
        NSLog(@"%@",_queuePlayer.items);
        if (_index == _items.count - 1) {
            _index = 0;
        } else {
            _index++;
        }
        [_queuePlayer advanceToNextItem];
        NSLog(@"%@",_queuePlayer.items);
        if (_queuePlayer.items.count == 0) {
            [self palyAtIndex:_index];
        }
        NSLog(@"下一首");
    }];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Black Eyed Peas-The Apl Song" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Avril Lavigne-Hello Kitty" ofType:@"mp3"];
    NSURL *url1 = [NSURL fileURLWithPath:path1];
    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"葛雨晴,Jelly筱洋-小尾巴" ofType:@"mp3"];
    NSURL *url2 = [NSURL fileURLWithPath:path2];
    
    _songItem = [[AVPlayerItem alloc] initWithURL:url];
    _songItem1 = [[AVPlayerItem alloc] initWithURL:url1];
    _songItem2 = [[AVPlayerItem alloc] initWithURL:url2];
    _items = @[_songItem,_songItem1,_songItem2];
    self.queuePlayer = [[AVQueuePlayer alloc] initWithItems:_items];
    self.slider.value = 0;
    self.slider.minimumValue = 0;
    __weak ViewController *vc = self;
    __weak AVQueuePlayer *queuePlayer = self.queuePlayer;
    [self.queuePlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float current = CMTimeGetSeconds(time);
        long long int total = CMTimeGetSeconds([queuePlayer.currentItem duration]);
        vc.slider.value = current / total;
//            vc.slider.maximumValue = total;
    }];
    [_slider addTarget:self action:@selector(sliderAction3) forControlEvents:UIControlEventValueChanged];
}

- (void)palyAtIndex:(NSInteger)index {
    [_queuePlayer removeAllItems];
    for (NSInteger i = index; i < _items.count; i++) {
        AVPlayerItem *objc = _items[i];
        if ([_queuePlayer canInsertItem:objc afterItem:nil]) {
            [objc seekToTime:kCMTimeZero];
            [_queuePlayer insertItem:objc afterItem:nil];
        }
    }
}
- (void)sliderAction3 {
    [_queuePlayer pause];
    long long int total = CMTimeGetSeconds([_queuePlayer.currentItem duration]);
    NSInteger drageSeconds = floorf(total * _slider.value);
    [_queuePlayer seekToTime:CMTimeMake(drageSeconds, 1) completionHandler:^(BOOL finished) {
        [_queuePlayer play];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)sliderAct:(id)sender {
    
}
@end
